package com.jx.mst.third.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.jx.mst.third.common.dtos.ResponseResult;
import com.jx.mst.third.service.FileStorageService;
import com.jx.mst.third.service.ImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class ImgServiceImpl implements ImgService {
    @Autowired
    FileStorageService fileStorageService;

    @Override
    public String UploadImg(MultipartFile file) {
        if (file == null || file.isEmpty() || file.getSize() == 0) {

        }
        String originalFilename = file.getOriginalFilename();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYYMMddHHmmss");
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(dateTimeFormatter);
        String randomString = RandomUtil.randomString(8);
        String prestr = format + "-" + randomString;
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String imgUrl;
        try {
            imgUrl = fileStorageService.uploadImgFile("", prestr + substring, file.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return imgUrl;
    }
//    @Override
//    public ResponseResult UploadImg(MultipartFile file) {
//        if (file == null || file.isEmpty() || file.getSize() == 0) {
//
//        }
//        String originalFilename = file.getOriginalFilename();
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYYMMddHHmmss");
//        LocalDateTime now = LocalDateTime.now();
//        String format = now.format(dateTimeFormatter);
//        String randomString = RandomUtil.randomString(8);
//        String prestr = format + "-" + randomString;
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String imgUrl;
//        try {
//            imgUrl = fileStorageService.uploadImgFile("", prestr + substring, file.getInputStream());
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        return ResponseResult.okResult(imgUrl);
//    }
}
